<template>
  <div>
    <van-button
      style="position:fixed;top:30px;right:0;z-index:999"
      type="primary"
    >===loading:{{ loading }}==finished:{{ finished }}==refreshing:{{refreshing}}</van-button>
    <van-pull-refresh @refresh="onRefresh" v-model="refreshing">
      <van-list
        :finished="finished"
        @load="onLoad"
        finished-text="没有更多了"
        offset="100"
        style="padding-bottom:30px"
        v-model="loading"
      >
        <van-cell
          :key="item.goods_id"
          :title="index+'===='+item.goods_name"
          v-for="(item,index) in list"
        />
      </van-list>
    </van-pull-refresh>
    <!-- <h1 :key="item.goods_id" style="background:pink" v-for="item in list">{{ item.goods_name}}</h1> -->
  </div>
</template>
<script>
import axios from 'axios'
export default {
  name: '',
  data () {
    return {
      pagenum: 0,
      pagesize: 10,
      list: [],
      refreshing: false,
      loading: false,//上拉加载触发就会true,
      finished: false,//不能上拉了true  还能上拉 false
    }
  },
  created () {

  },
  computed: {
  },
  methods: {
    onLoad () {
      /* 上拉就会触发 */
      console.log('onLoad');
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(() => {
      //   for (let i = 0; i < 10; i++) {
      //     this.list.push(this.list.length + 1);
      //   }

      //   // 加载状态结束
      //   this.loading = false;

      //   // 数据全部加载完成
      //   if (this.list.length >= 40) {
      //     this.finished = true;
      //   }
      // }, 1000);
      if (this.list.length >= 40) {
        this.finished = true;
        return
      }
      this.pagenum++
      this.getData()


    },
    async getData () {
      const res = await axios({
        url: 'http://vue-shop-api-t.itheima.net/api/private/v1/goods',
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize
        },
        headers: {
          Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2ODU0OTUxOTcsImV4cCI6MTY4NTU4MTU5N30.Hg6SFZXqVa5k7BJhXh24LcQqyWbQG3SlKlRtvU4KT3M'
        }
      })
      console.log(res, 888);
      this.list = this.refreshing ? res.data.data.goods : [...this.list, ...res.data.data.goods]
      this.loading = false;//关闭上拉加载
      this.refreshing = false
    },
    onRefresh () {
      // console.log('onRefresh');
      // // 清空列表数据
      // this.finished = false;

      // // 重新加载数据
      // // 将 loading 设置为 true，表示处于加载状态
      // this.loading = true;
      // this.onLoad();
      /* 数据获取第一页 */
      this.pagenum = 0
      this.loading = true
      this.onLoad()
    },
  }
}
</script>
<style lang='less'  scoped>
</style>